const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    },
    fileName: {
      type: String,
      value: "file_name"
    },
    filePath: {
      type: String,
      value: "file_path"
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    baseUrl,
    commonUpdate,
    commonUpload,
    fileView: {

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    viewFile(e) {
      console.log(e)
      const file_path = baseUrl + commonUpload + e.currentTarget.dataset.item
      const id = file_path
      if (this.data.fileView[id]) {
        this.openView(this.data.fileView[id])
      } else {
        wx.showLoading({
          title: '加载中...',
          success: () => {
            wx.downloadFile({
              url: file_path,
              success: (res) => {
                const filePath = res.tempFilePath
                this.data.fileView[id] = filePath
                wx.hideLoading()
                this.openView(filePath)
              },
              fail: () => {
                wx.showToast({
                  title: '文件下载失败！',
                  icon: "error"
                })
              }
            })
          }
        })
      }

    },
    // 只能手机端进行设备识别
    openView(path) {
      wx.openDocument({
        filePath: path,
        success: function (res) {

        },
        fail: () => {
          wx.showToast({
            title: '文件打开失败！',
            icon: "error"
          })
        }
      })
    },
  }
})